package com.zqxx.examine.management.controller.action.teacher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.TrainingService;
import com.zqxx.examine.model.exam.Message;
import com.zqxx.examine.model.training.Training;
import com.zqxx.examine.model.training.TrainingSection;

@Controller
public class TrainingActionTeacher
{
  @Autowired
  private TrainingService trainingService;
  
  @RequestMapping(value={"teacher/training/add-training"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addTraining(@RequestBody Training training)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
    Message msg = new Message();
    try
    {
      training.setCreatorId(userInfo.getUserid());
      this.trainingService.addTraining(training);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/training/add-training-section"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addTrainingSection(@RequestBody TrainingSection section)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
    Message msg = new Message();
    try
    {
      section.setUserId(userInfo.getUserid());
      this.trainingService.addTrainingSection(section);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @ResponseBody
  @RequestMapping(value={"teacher/training/del-training/{trainingId}"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  public Message delTraining(@PathVariable("trainingId") int trainingId)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Message msg = new Message();
    try
    {
      this.trainingService.deleteTraining(trainingId, userInfo.getUserid());
    }
    catch (Exception e)
    {
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @ResponseBody
  @RequestMapping(value={"teacher/training/del-training-section/{sectionId}"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  public Message delTrainingSection(@PathVariable("sectionId") int sectionId)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Message msg = new Message();
    try
    {
      this.trainingService.deleteTrainingSection(sectionId, userInfo.getUserid());
    }
    catch (Exception e)
    {
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
}
